package com.zlp.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.modules.pms.dto.resp.ProductAttributeCategoryItemResp;
import com.zlp.mall.modules.pms.dto.resp.ProductAttributeCategoryResp;
import com.zlp.mall.modules.pms.model.ProductAttributeCategory;
import com.zlp.mall.modules.pms.service.ProductAttributeCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/productAttribute/category")
@Api(value = "ProductAttributeCategoryController", tags = "商品属性分类模块")
public class ProductAttributeCategoryController {

    private final ProductAttributeCategoryService productAttributeCategoryService;



    @GetMapping(value = "/list")
    @ApiOperation(value = "根据商品属性分类列表")
    public R<Pager<ProductAttributeCategoryResp>> getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        Pager<ProductAttributeCategoryResp> page = productAttributeCategoryService.list(pageNum, pageSize);
        return R.success(page);
    }

    @GetMapping(value="/list/withAttr")
    @ApiOperation(value = "属性下拉级联数据")
    public R getListWithAttr(){

        List<ProductAttributeCategoryItemResp> list = productAttributeCategoryService.getListWithAttr();
        return R.success(list);
    }


    @PostMapping(value = "/create")
    @ApiOperation("添加商品属性分类")
    public R create(@RequestParam String name) {

        return R.success(productAttributeCategoryService.create(name));
    }

    @ApiOperation("修改商品属性分类")
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable Long id, @RequestParam String name) {
        boolean result = productAttributeCategoryService.update(id, name);
        return R.success(result);
    }

    @ApiOperation("删除单个商品属性分类")
    @GetMapping(value = "/delete/{id}")
    public R delete(@PathVariable Long id) {
        boolean result = productAttributeCategoryService.delete(id);
        return R.success(result);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation("获取单个商品属性分类信息")
    public R<ProductAttributeCategoryResp> getItem(@PathVariable Long id) {

        ProductAttributeCategoryResp productAttributeCategory = productAttributeCategoryService.getItem(id);
        return R.success(productAttributeCategory);
    }

}

